<?php include("includes/session.php"); ?>
<?php include("includes/connect_db.php"); ?>
<?php include("includes/header.php"); ?>
<?php include("notif.php"); ?>

<style>
	input{
		width: 400px;
	}
	.borderTop{
		border-top: 1px solid #08c;
		padding-top: 20px;
	}
</style>

<?php
	$sql_select_gammu = "SELECT * FROM tb_setting_gammu";
	$query_select_gammu = mysql_query($sql_select_gammu);
	$hitung_baris = mysql_num_rows($query_select_gammu);

	if ($hitung_baris == 0) {
		$lokasi = "";
		$port = "";
		$koneksi = "";
		$pin = "";
	} else {
		$query_select_gammu = mysql_query($sql_select_gammu);
		$select_gammu = mysql_fetch_array($query_select_gammu);
	}

	/*passthru("net start > service.log");
	$handle = fopen("service.log", "r");
	$status = 0;

	while (!feof($handle)) {
	   $baristeks = fgets($handle);
	   if (substr_count($baristeks, 'Gammu SMSD Service (GammuSMSD)') > 0) {
	    	$status = 1;
	   }
	}
	fclose($handle);

	if ($status == 0) {
		passthru($lokasi."\bin\gammu-smsd -c gammurc -s");
	}*/
?>

<div class="span9">  
    <h3>SMS</h3>                      
    <ul class="breadcrumb well">
        <li class="active">Gammu</li>                
    </ul>    
    

	<form class="form-horizontal" action="" method="post">
    	<div>
    		<div>
    			<div class="alert alert-info">
					Setting Gammu
			    </div>
				<div class="control-group">
					<label class="control-label">Lokasi Penyimpanan</label>
					<div class="controls">
						<input placeholder="Contoh: C:\Gammu" type="text" name="lokasi" class="lokasi" value="<?php echo $select_gammu['lokasi'];?>">
					</div>
				</div>
				<div class="control-group">
					<label class="control-label">Log User</label>
					<div class="controls">
						<input placeholder="Masukkan Log User" type="text" name="log" class="log" value="<?php echo $select_gammu['nama_log'] ?>">
					</div>
				</div>
				<div class="control-group">
					<label class="control-label">Pin</label>
					<div class="controls">
						<input placeholder="Masukkan Pin User" type="text" name="pin" class="pin" value="<?php echo $select_gammu['pin'] ?>">
					</div>
				</div>
    		</div>

    		<div>
    			<div class="alert alert-info">
					Setting Database
			    </div>
				<div class="control-group">
					<label class="control-label">Username Host</label>
					<div class="controls">
						<input placeholder="Masukkan Username Host" type="text" name="user" class="user" value="<?php echo $select_gammu['user'] ?>">
					</div>
				</div>
				<div class="control-group">
					<label class="control-label">Password Host</label>
					<div class="controls">
						<input placeholder="Masukkan Password Host" type="password" name="pass" class="pass" value="<?php echo $select_gammu['pass'] ?>">
					</div>
				</div>
				<div class="control-group">
					<label class="control-label">Alamat Host</label>
					<div class="controls">
						<input placeholder="Masukkan Alamat Host" type="text" name="host" class="host" value="<?php echo $select_gammu['host'] ?>">
					</div>
				</div>
				<div class="control-group">
					<label class="control-label">Database</label>
					<div class="controls">
						<input placeholder="Masukkan Database" type="text" name="db" class="db" value="<?php echo $select_gammu['db'] ?>">
					</div>
				</div>
    		</div>

    		<div id="InputModem">
    			<div class="alert alert-info">
					Setting Modem
					<button class="btn btn-info" id="tambahModem">
						<i class="icon-white icon-plus"></i>
						Tambah Modem
					</button>
			    </div>
			    <?php
			    	$i = 0;
			    	$sql_cari_data = "SELECT * FROM tb_setting_gammu_detail";
			    	$query_cari_data = mysql_query($sql_cari_data);
			    	while ($cari_data = mysql_fetch_array($query_cari_data)) {
			    ?>
			    <div class="Tambahan">
		    		<?php 
		    			if ($i > 0) {
		    		?>
		    		<div class="control-group borderTop">
		    		<?php
		    			} else {
		    		?>
					<div class="control-group">
		    		<?php
		    			}
		    		?>
						<label class="control-label">Port</label>
						<div class="controls">
							<input placeholder="Masukkan Port Alat Anda" type="text" name="port" class="port" value="<?php echo $cari_data['port'] ?>" data-index="<?php echo $i; ?>">
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">Koneksi Alat</label>
						<div class="controls">
							<input placeholder="Masukkan Koneksi Alat" type="text" name="koneksi" class="koneksi" value="<?php echo $cari_data['koneksi'] ?>">
						</div>
					</div><div class="control-group">
						<label class="control-label">ID Alat</label>
						<div class="controls">
							<input placeholder="Masukkan Id Alat" type="text" name="nama" class="nama" value="<?php echo $cari_data['nama_alat'] ?>">
							<?php 
				    			if ($i > 0) {
									echo "<a style='margin-left: 20px;' class='removeTabel' href='#'>Remove</a>";
				    			}
				    		?>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">Provider</label>
						<div class="controls">
							<select class="provider" name="provider">
								<option name="provider" value="*">- Pilih provider -</option>
								<?php 
									$sql_provider = "SELECT * FROM tb_provider";
									$query_provider = mysql_query($sql_provider);
									$i == 1;
									while ($provider = mysql_fetch_array($query_provider)) {
										$selected = "";
										if ($cari_data['id_provider'] == $provider['id_provider']) {
											$selected = "selected='selected'";
										}
										echo "<option name='provider' value='$provider[id_provider]' $selected>".$provider['provider']."</option>";
									}
								?>
							</select>
						</div>
					</div>
					</div>
			    <?php
			    		$i++;
			    	}
			    ?>
    		</div>
		</div>
	</form>
		<div>
			<div class="span12">
				<ul class="breadcrumb well">
					<button class="btn btn-info simpan" name="simpan">
						<i class="icon-white icon-ok"></i>
						Simpan
					</button>
				</ul>
			</div>
		</div>

	<div id="valuePlacer">
		<input type="hidden" id="dataLokasi" value="<?php echo $select_gammu['lokasi'] ?>">
		<input type="hidden" id="dataLog" value="<?php echo $select_gammu['nama_log'] ?>">
		<input type="hidden" id="dataPin" value="<?php echo $select_gammu['pin'] ?>">
		<input type="hidden" id="dataUser" value="<?php echo $select_gammu['user'] ?>">
		<input type="hidden" id="dataPass" value="<?php echo $select_gammu['pass'] ?>">
		<input type="hidden" id="dataHost" value="<?php echo $select_gammu['host'] ?>">
		<input type="hidden" id="dataDb" value="<?php echo $select_gammu['db'] ?>">
	</div>
</div>

<?php
	// if(isset($_POST['simpan'])){
	// 	$lokasi = $_POST['lokasi'];
	// 	$port = $_POST['port'];
	// 	$koneksi = $_POST['koneksi'];
	// 	$pin = $_POST['pin'];
	// 	$log = $_POST['log'];
	// 	$logFile = $log.".txt";
	// 	$log_lama = $select_gammu['nama_log'];
	// 	$logFile_lama = $log_lama.".txt";
	// 	$user = $_POST['user'];
	// 	$pass = $_POST['pass'];
	// 	$host = $_POST['host'];
	// 	$db = $_POST['db'];
	// 	$log_dir = $lokasi."\bin\\".$logFile;
	// 	$isi_log = "";

	// 	if (empty($lokasi) or empty($port) or empty($koneksi) or empty($pin) or empty($log) or empty($user) or empty($host) or empty($db)){
 //            echo "<SCRIPT> alert('Isi Data Terlebih dahulu');</SCRIPT>";
 //        } else{
 //        	$sql_cek_gammu = "SELECT * FROM tb_setting_gammu";
 //        	$query_cek_gammu = mysql_query($sql_cek_gammu);
 //        	$cek_gammu = mysql_num_rows($query_cek_gammu);

	// 		/*passthru("net start > service.log");
	// 		$handle = fopen("service.log", "r");
	// 		$status = 0;

	// 		while (!feof($handle)) {
	// 		   $baristeks = fgets($handle);
	// 		   if (substr_count($baristeks, 'Gammu SMSD Service (GammuSMSD)') > 0) {
	// 		    	$status = 1;
	// 		   }
	// 		}
	// 		fclose($handle);

	// 		if ($status == 1) {
	// 			passthru($lokasi."\bin\gammu-smsd -k");
	// 		}*/

 //        	if ($cek_gammu != 0) {
 //        		// echo "<div style='display: none;'>";
 //        			passthru($lokasi."\bin\gammu-smsd -k");
 //        			passthru($lokasi."\bin\gammu-smsd -u");
 //        		// echo "</div>";
 //        	}

 //        	$sql_hapus_gammu = "TRUNCATE tb_setting_gammu";
 //        	$query_hapus_gammu = mysql_query($sql_hapus_gammu);
 //    		$sql_input_gammu = "INSERT INTO tb_setting_gammu (lokasi, port, koneksi, pin, nama_log, user, pass, host, db)
 //    							VALUES ('".addslashes($lokasi)."', '$port', '$koneksi', '$pin', '$log', '$user', '$pass', '$host', '$db')";
    		
	// 		if ($query_input_gammu = mysql_query($sql_input_gammu)) {
	// 			$gammu_dir = $lokasi."\bin\gammurc";
	// 			$isi = "[gammu]\r\n"."port = ".$port."\r\nconnection = ".$koneksi."\r\n\r\n[smsd]\r\nservice=sql\r\ndriver=native_mysql\r\nPIN=".$pin."\r\nLogFile=".$log_dir."\r\nuser=".$user."\r\npassword=".$pass."\r\nhost=".$host."\r\ndatabase=".$db."\r\n";

	// 			$file_gammu = fopen($gammu_dir, "w");
	// 		 	fputs($file_gammu, "$isi");
	// 		 	fclose($file_gammu);
				
	// 			$log_dir_lama = $lokasi."\bin\\".$logFile_lama;
	// 			if(file_exists($log_dir_lama)) {
	// 				$hapus=unlink($log_dir_lama);
	// 			}

	// 			$file_log = fopen($log_dir, "w");
	// 		 	fputs($file_log, "$isi_log");
	// 		 	fclose($file_log);

	// 			// echo "<div style='display: none;'>";
        			
 //        			//passthru($lokasi."\bin\gammu identify");
	// 				passthru($lokasi."\bin\gammu-smsd -c gammurc -i");
 //        			passthru($lokasi."\bin\gammu-smsd -c gammurc -s");
 //        		// echo "</div>";	
	// 			echo "<SCRIPT> alert('Data Disimpan'); location.href='gammu.php';</SCRIPT>";
	// 		}
	//   	}
	// }
?>

<script type="text/javascript">
	var portArr = [<?php
						$i = 1;
						$sql_cari_data = "SELECT * FROM tb_setting_gammu_detail";
						$query_cari_data = mysql_query($sql_cari_data);
						$jumlah = mysql_num_rows($query_cari_data);
						$query_cari_data = mysql_query($sql_cari_data);
						while ($cari_data = mysql_fetch_array($query_cari_data)) {
					?>
							['<?php echo $cari_data[port] ?>', '<?php echo $cari_data[koneksi] ?>', '<?php echo $cari_data[nama_alat] ?>', '<?php echo $cari_data[id_provider] ?>']
					<?php	
							if ($i < $jumlah) {
								echo ",";
							};
							$i++;	
						}
					?>
				];
	<?php
		$sql_cari_data = "SELECT * FROM tb_setting_gammu_detail";
		$query_cari_data = mysql_query($sql_cari_data);
		$jumlah = mysql_num_rows($query_cari_data);
	?>
	var x = <?php echo $jumlah ?>;
	var port = false;
	var koneksi = false;
	var nama = false;
	var provider = false;

	function cekIsi(port, koneksi, nama, provider, portEl, koneksiEl, namaEl, providerEl){
		if (port && koneksi && nama && provider) {
        	if(portArr.length==0){
	        	portArr.push([portEl.val(), koneksiEl.val(), namaEl.val(), providerEl.val()]);
        	}else if(portArr[portEl.data('index')] == undefined){
        		portArr[portEl.data('index')] = [portEl.val(), koneksiEl.val(), namaEl.val(), providerEl.val()];
        	}else{
        		portArr[portEl.data('index')] = [portEl.val(), koneksiEl.val(), namaEl.val(), providerEl.val()];
        	}
        }
        port = false;
    	koneksi = false;
    	nama = false;
    	provider = false;
	}

	$(document).ready(function() {

		if(portArr[0][0]!=""){
			port = true;
		}
		if(portArr[0][1]!=""){
			koneksi = true;
		}
		if(portArr[0][2]!=""){
			nama = true;
		}
		if(portArr[0][3]!="*"){
			provider = true;
		}

		$('#tambahModem').live('click', function(){
			var selProv = $('.provider:first').parent('div').html();
			console.log(selProv);
			$('#InputModem').append(
				"<div class='Tambahan'>"+
					"<div class='control-group borderTop'>"+
						"<label class='control-label'>Port</label>"+
						"<div class='controls'>"+
							"<input class='port' placeholder='Masukkan Port Alat Anda' type='text' name='port' data-index='"+x+"'>"+
						"</div>"+
					"</div>"+
					"<div class='control-group'>"+
						"<label class='control-label'>Koneksi Alat</label>"+
						"<div class='controls'>"+
							"<input class='koneksi' placeholder='Masukkan Koneksi Alat' type='text' name='koneksi'>"+
						"</div>"+
					"</div>"+
					"<div class='control-group'>"+
						"<label class='control-label'>Id Alat</label>"+
						"<div class='controls'>"+
							"<input class='nama' placeholder='Masukkan ID Alat' type='text' name='nama'>"+
							"<a style='margin-left: 20px;' class='removeTabel' href='#'>Remove</a>"+
						"</div>"+
					"</div>"+
					"<div class='control-group'>"+
						"<label class='control-label'>Provider</label>"+
						"<div class='controls'>"+
							selProv+
						"</div>"+
					"</div>"+
				"<div>"
            );
            x++;
            return false;

		});

		$('.removeTabel').live('click', function(){
			$(this).parent('div').parent('div').parent('div').remove();
			portArr.splice($(this).closest('.Tambahan').find('.port').data('index'),1);
			return false;
        });

        $('.lokasi').keyup(function(){
        	$('#dataLokasi').val($(this).val());
        });

        $('.log').keyup(function(){
        	$('#dataLog').val($(this).val());
        });

        $('.pin').keyup(function(){
        	$('#dataPin').val($(this).val());
        });

        $('.user').keyup(function(){
        	$('#dataUser').val($(this).val());
        });

        $('.pass').keyup(function(){
        	$('#dataPass').val($(this).val());
        });

        $('.host').keyup(function(){
        	$('#dataHost').val($(this).val());
        });

        $('.db').keyup(function(){
        	$('#dataDb').val($(this).val());
        });

        $('.port').live('blur', function(){
        	var thatA = $(this);
        	var thatB = thatA.closest('.Tambahan').find('.koneksi');
        	var thatC = thatA.closest('.Tambahan').find('.nama');
        	var thatD = thatA.closest('.Tambahan').find('.provider');
        	if(thatB.val()==""){
        		koneksi = false;
        	}
        	if (thatC.val()=="") {
        		nama = false;
        	}
        	if (thatD.val()=="*") {
        		provider = false;
        	}
        	if(thatA.val()!=""){
        		port = true;
        		cekIsi(port, koneksi, nama, provider, thatA, thatB, thatC, thatD);
        	}
        });

        $('.koneksi').live('blur', function(){
        	var thatA = $(this);
        	var thatB = thatA.closest('.Tambahan').find('.port');
        	var thatC = thatA.closest('.Tambahan').find('.nama');
        	var thatD = thatA.closest('.Tambahan').find('.provider');
        	if(thatB.val()==""){
        		port = false;
        	}
        	if (thatC.val()=="") {
        		nama = false;
        	}
        	if (thatD.val()=="*") {
        		provider = false;
        	}
        	if(thatA.val()!=""){
        		koneksi = true;
        		cekIsi(port, koneksi, nama, provider, thatB, thatA, thatC, thatD);
        	}
        });

        $('.nama').live('blur', function(){
        	var thatA = $(this);
        	var thatB = thatA.closest('.Tambahan').find('.port');
        	var thatC = thatA.closest('.Tambahan').find('.koneksi');
        	var thatD = thatA.closest('.Tambahan').find('.provider');console.log(thatD.val());
        	if(thatB.val()==""){
        		port = false;
        	}
        	if(thatC.val()==""){
        		koneksi = false;
        	}
        	if(thatD.val()=="*"){
        		provider = false;
        	}
        	if(thatA.val()!=""){
        		nama = true;
        		cekIsi(port, koneksi, nama, provider, thatB, thatC, thatA, thatD);
        	}
        });

        $('.provider').live('change', function(){
        	console.log($(this).val());
        	var thatA = $(this);
        	var thatB = thatA.closest('.Tambahan').find('.port');
        	var thatC = thatA.closest('.Tambahan').find('.koneksi');
        	var thatD = thatA.closest('.Tambahan').find('.nama');
        	if(thatB.val()==""){
        		port = false;
        	}
        	if(thatC.val()==""){
        		koneksi = false;
        	}
        	if(thatD.val()==""){
        		nama = false;
        	}
        	if(thatA.val()!="*"){
        		provider = true;
        		cekIsi(port, koneksi, nama, provider, thatB, thatC, thatD, thatA);
        	}
        });

        $('.simpan').click(function(){
        	var form_gammu = {
        		lokasi : $('#dataLokasi').val(),
        		log : $('#dataLog').val(),
        		pin : $('#dataPin').val(),
        		user : $('#dataUser').val(),
        		pass : $('#dataPass').val(),
        		host : $('#dataHost').val(),
        		db : $('#dataDb').val(),
        		gammu : portArr
			};
			$.ajax({
				type: "POST",
				url: "act_gammu.php",
				data: form_gammu, 
				cache: false, 
				success: function(msg){
					window.location = 'gammu.php';
				} 
			});
        });
	});
</script>

<?php include("includes/footer.php"); ?>
